#!/bin/bash
# Update DB from dcm4chee-2.9.x to dcm4chee-2.10.x

db2 CONNECT TO tiani
db2 SET SCHEMA tiani

tables="ae code patient study mpps series series_req \
        media instance filesystem study_on_fs files  \
	mwl_item gpsps gpsps_perf gpsps_req gppps hp \
	hpdef priv_patient priv_study priv_series \
	priv_instance priv_file device audit_record"
	
function bailout()
{
  echo "DB error occurred"
  db2 TERMINATE
  exit -1
}

for t in $tables ; 
do
  echo "=== Alter table $t ==="
  db2 -v "ALTER TABLE $t ALTER COLUMN pk SET GENERATED BY DEFAULT AS IDENTITY" || bailout
  pk_start=`db2 -x "VALUES(NEXTVAL FOR ${t}_pk_seq)"`
  db2 -v "ALTER TABLE $t ALTER COLUMN pk RESTART WITH $pk_start" || bailout
  db2 -v "DROP SEQUENCE ${t}_pk_seq" || bailout
done

db2 -stf update-2.10.db2sql

db2 TERMINATE

